package operation;

import book.Book;
import book.BookList;

import java.util.Scanner;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 阿宾
 * Date: 2024-07-22
 * Time: 16:45
 */
public class AddOperation implements IOperation {
    @Override
    public void work(BookList bookList) {
        System.out.println("新增图书！");

        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入书名：");
        String name = scanner.nextLine();
        System.out.println("请输入作者：");
        String author = scanner.nextLine();
        System.out.println("请输入价格：");
        int price = scanner.nextInt();
        scanner.nextLine();//因为下边是字符串类型，上边是整型类型，会把上边输入的回车读入，导致下边的类型不能输入
        System.out.println("请输入类型：");
        String type = scanner.nextLine();

        Book book = new Book(name,author,price,type);

        int currentSize = bookList.getUsedSize();
        //如果有这个书，就不能添加了
        for (int i = 0; i < currentSize; i++) {
            Book book1 = bookList.getBook(i);
            if(book1.getName().equals(name)){
                System.out.println("书架存在这本书，不能进行添加");
                return;
            }
        }
        //添加书
        bookList.setBooks(currentSize,book);
        //更新书架上书的个数
        bookList.setUsedSize(currentSize+1);
    }
}
